package com.nowcoder.Offer.List;

import org.junit.Test;

import java.util.HashMap;

/**
 * 复杂链表的复制
 */
public class CopyRandomNode {
    public class RandomListNode {
        int label;
        RandomListNode next = null;
        RandomListNode random = null;

        RandomListNode(int label) {
            this.label = label;
        }
    }

    public RandomListNode Clone(RandomListNode pHead){
        HashMap<RandomListNode,RandomListNode> map = new HashMap<>();

        RandomListNode pNode = pHead;

        while(pNode!=null){
            map.put(pNode,new RandomListNode(pNode.label));
            pNode = pNode.next;
        }

        pNode = pHead;

        while(pNode!=null){
            map.get(pNode).next = map.get(pNode.next);
            map.get(pNode).random = map.get(pNode.random);
            pNode = pNode.next;
        }

        return map.get(pHead);
    }

    @Test
    public void test(){

    }
}



















